﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;

namespace Evolution
{
    /// <summary>
    /// There are recorded options for one animal type.
    /// </summary>
    class TypeInfo
    {
        public byte MinValueToLive, MaxValueToLive;
        public byte MinValueToBorn;

        public void PrintValues(byte Percent)
        {
            Console.Write("\t"); Console.Write(Percent); Console.Write("% of all cells.\n");
            Console.Write("\t"); Console.Write(MinValueToLive); Console.Write(" - min value to live .\n");
            Console.Write("\t"); Console.Write(MaxValueToLive); Console.Write(" - max value to live .\n");
            Console.Write("\t"); Console.Write(MinValueToBorn); Console.Write(" - min value to born .\n");
        }

        /// <summary>
        /// Save to file
        /// </summary>
        /// <param name="F">File, recives data.</param>
        public void Save(ref StreamWriter F)
        {
            F.WriteLine("\tMinValueToLive = " + Convert.ToString(MinValueToLive));
            F.WriteLine("\tMaxValueToLive = " + Convert.ToString(MaxValueToLive));
            F.WriteLine("\tMinValueToBorn = " + Convert.ToString(MinValueToBorn));
        }

        public TypeInfo(byte Min, byte Max, byte Birth)
        {
            this.MaxValueToLive = Max;
            this.MinValueToLive = Min;
            this.MinValueToBorn = Birth;
        }

        /// <summary>
        /// Reading from console.
        /// </summary>
        public TypeInfo(string St)
        {
            Console.Write("Enter min near for "); Console.Write(St); Console.Write(" :");
            Byte.TryParse(Console.ReadLine(), out MinValueToLive);
            Console.Write("Enter max near for "); Console.Write(St); Console.Write(" :");
            Byte.TryParse(Console.ReadLine(), out MaxValueToLive);
            Console.Write("Enter min near to birth "); Console.Write(St); Console.Write(" :");
            Byte.TryParse(Console.ReadLine(), out MinValueToBorn);
        }


        /// <summary>
        /// Read from file.
        /// </summary>
        public TypeInfo(ref FileStream FileIN)
        {
            MinValueToLive = (byte)FileIN.ReadByte();
            MaxValueToLive = (byte)FileIN.ReadByte();
            MinValueToBorn = (byte)FileIN.ReadByte();
        }

        /// <summary>
        /// Returns new value of the cell. It is either "OldValue" or 0 (death).
        /// Eating is not counted here.
        /// </summary>
        /// <param name="OldValue">Old value of the cell.</param>
        /// <param name="NearCount">Near cells count. These cells may be same type.</param>
        /// <returns>New value of the cell.</returns>
        public byte Death(byte OldValue, byte NearCount)
        {
            return (byte)((NearCount < MinValueToLive) || (NearCount > MaxValueToLive) ? 0 : OldValue);
        }
    }
}
